package com.tyd.cams.ui.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.google.gson.Gson;
import com.tyd.cams.Adapter.Gridadapter;
import com.tyd.cams.Adapter.TzggAdapter;
import com.tyd.cams.LoginActivity;
import com.tyd.cams.MainsActivity;
import com.tyd.cams.R;
import com.tyd.cams.entity.Gridp;
import com.tyd.cams.entity.LoginBean;
import com.tyd.cams.entity.SkTzgg;
import com.tyd.cams.entity.SkXyjg;
import com.tyd.cams.utils.SPUtils;
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem;
import com.xuexiang.xui.widget.banner.widget.banner.SimpleImageBanner;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class HomeFragment extends Fragment {

    private SimpleImageBanner mSimpleImageBanner;
    private ListView SkTzggListView;
    private GridView gridView;
    private ArrayList<Gridp> gridps;
    private Gridadapter gridadapter;
    ArrayList<SkTzgg> list = new ArrayList<>();

    //banner 中的标题
    public static String[] titles = new String[]{
            "沈阳科技学院",
            "博学笃行",
            "种德修身",
    };
    //banner 的图片资源
    public static String[] urls = new String[]{//640*360 360/640=0.5625
            "http://img.alphaunion.cn/banner1.png",
            "http://img.alphaunion.cn/banner2.jpg",
            "http://img.alphaunion.cn/banner3.jpg",
    };
    private List<BannerItem> mData;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        mSimpleImageBanner = root.findViewById(R.id.sib_simple_banner);
        //通知公告开始Start
        gettzgg();
        /*自定义Banner字体颜色*/
        int coler = Color.rgb(225, 107, 205);
        mSimpleImageBanner.setTextColor(coler);
        mSimpleImageBanner.setTextSize(20);
        initData();
        sib_simple_usage();
        return root;
    }

    /*通知公告List获取开始*/
    private void gettzgg() {
        new Thread() {
            public void run() {
                OkHttpUtils.post()
                        .url("http://www.syist.edu.cn/Notice/index/cid/")
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Toast.makeText(getActivity(), "通知公告链接服务器超时", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(String response, int id) {
//                                Log.i("获取的返回信息", response);
                                Document document = Jsoup.parse(response);
                                Elements context = document.select("body > div.main > div.p-r > div.mainbox > ul > li");
//                                String imgurl = document.select("#picbox > li").select("img").attr("src");
                                for (Element element : context) {
                                    String time = element.select("span").text();
                                    String title = element.select("a").text().substring(0, element.select("a").text().indexOf("】")) + "】";
                                    String con = element.select("a").text().substring(element.select("a").text().indexOf("】") + 1);
                                    String href = element.select("a").attr("href");

                                    SkTzgg tzgg = new SkTzgg(time, title, con, "http://www.syist.edu.cn" + href);
                                    list.add(tzgg);
                                }
                                SkTzggListView = getActivity().findViewById(R.id.SkTzggListView);
                                TzggAdapter tzggAdapter = new TzggAdapter(getActivity(), list);
                                SkTzggListView.setAdapter(tzggAdapter);
                                /*SkTzggListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                       *//* Bundle bundle = new Bundle();
                                        bundle.putString("url", String.valueOf(list.get(position)));
                                        Intent intent = new Intent();
                                        intent.putExtras(bundle);
                                        intent.setClass(getContext(), MoveList.class);
                                        startActivity(intent);*//*
                                        System.out.println("你好"+list.get(position).toString());
                                    }
                                });*/
                                tzggAdapter.notifyDataSetChanged();
                                Log.i("通知公告信息", list.toString());
                                for (SkTzgg skTzgg : list) {
                                    System.out.println(skTzgg);
                                }

                            }
                        });
            }
        }.start();
    }

    /*主页菜单按钮*/
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        gridView = getActivity().findViewById(R.id.mc_list);
        iconList();
        gridadapter = new Gridadapter(this.getContext(), gridps);
        gridView.setAdapter(gridadapter);
    }

    private void iconList() {
        gridps = new ArrayList<>();
        gridps.add(new Gridp("专业成绩", R.drawable.iconcj));
        gridps.add(new Gridp("学生饭卡", R.drawable.iconfanka));
        gridps.add(new Gridp("国考查询", R.drawable.icongjcx));
        gridps.add(new Gridp("学校教室", R.drawable.iconjaoshi));
        if (SPUtils.getUsertype().equals("1")) {
            gridps.add(new Gridp("班级管理", R.drawable.iconbanjixx));
            gridps.add(new Gridp("学校资讯", R.drawable.kebiao));
            gridps.add(new Gridp("教务公告", R.drawable.icongonggao));
            gridps.add(new Gridp("签到打卡", R.drawable.q3));
        } else {
            gridps.add(new Gridp("学校资讯", R.drawable.kebiao));
            gridps.add(new Gridp("教务公告", R.drawable.icongonggao));
            gridps.add(new Gridp("签到打卡", R.drawable.q3));
        }

    }

    /*主页菜单按钮结束*/
    /*Banner 开始*/
    private void sib_simple_usage() {
        mSimpleImageBanner.setSource(mData)
                .setOnItemClickListener((view, item, position) -> {
                })
                .setIsOnePageLoop(false)//设置当页面只有一条时，是否轮播
                .startScroll();//开始滚动
    }

    private void initData() {
        mData = new ArrayList<>(urls.length);
        for (int i = 0; i < urls.length; i++) {
            BannerItem item = new BannerItem();
            item.imgUrl = urls[i];
            item.title = titles[i];
            mData.add(item);
        }
    }
    /*Banner 结束*/
}